
function s (select) {
    return document.querySelector(select);
}
//登陆改变图标
function changeIcon(){
    var token = selectByKey("like_token");
    if(token){
        var imgs = document.getElementsByTagName("a")[1];
        imgs.style.backgroundImage = "url('../../statics/images/index/Group.png')";
        imgs.style.backgroundSize = "23px 23px";
        imgs.style.backgroundPosition = "-1px 0px";
        imgs.style.backgroundRepeat = "no-repeat";
    }
}
//触摸黑影
function touchst() {
    var lis = document.querySelector('#select').querySelectorAll('li');
    for (var i = 0; i < lis.length; i++) {
        lis[i].addEventListener("touchstart", function () {
            for (var o = 0; o < lis.length; o++)lis[o].className = '';
            this.className = 'drak';
            var that = this;
            document.addEventListener("touchend",function(){
                that.setAttribute("class","");
            })
        }, false);
    }
}
function getTimeNum (time) {
    var date = {}
    var preTime = time.replace(/\-/g,'/');
    var preTimeTurn = Date.parse(new Date(preTime));
    var nowDate = new Date().getTime();
    var timeCha = (nowDate - preTimeTurn)/1000;
    if(timeCha / 3600 < 1){
        date.num = parseInt(timeCha / 60);
        date.timeType = '分钟';
        return date;
    }else if(timeCha / 86400 < 1){
        date.num = parseInt(timeCha / 3600);
        date.timeType = '小时';
        return date;
    }else if(timeCha / 31104000 < 1){
        date.num = parseInt(timeCha / 86400);
        date.timeType = '天';
        return date;
    }else {
        date.num = parseInt(timeCha / 31104000);
        date.timeType = '年';
        return date;
    }
}
function ajaxHtml (data,htmlMode,htmlNode) {
    var html = '';
    for(var i = 0; i < data.length; i++){
        var htmlTemp = htmlMode.innerHTML;
        var timeObj = getTimeNum(data[i].created_at);
        htmlTemp = htmlTemp.replace(/\{\{time_num\}\}/,timeObj.num);
        htmlTemp = htmlTemp.replace(/\{\{hour_or_day\}\}/,timeObj.timeType);
        htmlTemp = htmlTemp.replace(/\{\{cid\}\}/,data[i].cid);
        htmlTemp = htmlTemp.replace(/\{\{id\}\}/,data[i].id);
        htmlTemp = htmlTemp.replace(/\{\{src\}\}/,data[i].thumb_pic);
        htmlTemp = htmlTemp.replace(/\{\{description\}\}/,data[i].title);
        html += htmlTemp;
    }
    htmlNode.innerHTML = html;
}
function videoAjax(data,htmlMode,htmlNode) {
    var html = '';
    for(var i = 0; i < 2; i++){
        var htmlTemp = htmlMode.innerHTML;
        htmlTemp = htmlTemp.replace(/\{\{videoId\}\}/,data[i].id);
        htmlTemp = htmlTemp.replace(/\{\{imageSrc\}\}/,data[i].thumb_pic);
        htmlTemp = htmlTemp.replace(/\{\{videoName\}\}/,data[i].title);
        html += htmlTemp;
    }
    htmlNode.innerHTML = html;
}
function shoucan() {
    var scs = document.querySelectorAll('.sc'), i = 0,path='../../statics/images/school';
    for (; i < scs.length; i++) {
        Tap(scs[i], function (e, dom) {
            var domN;
            if(dom.children[0] != undefined){
                domN = dom.children[0]
            }else{
                domN = dom;
            }
            var pngname = domN.src.split('/')[domN.src.split('/').length - 1];
            if (pngname == 'wsc.png') {
                domN.src = path + '/sc.png';
                //ajax({
                //    method: 'POST',
                //    url: '',
                //    data: {},
                //    success: function (response) {
                //        console.log(response)
                //    }
                //});
            } else {
                domN.src = path + '/wsc.png';
                //ajax({
                //    method: 'POST',
                //    url: '',
                //    data: {},
                //    success: function (response) {
                //        console.log(response)
                //    }
                //});
            }
        })
    }
};
//收藏模块END
function toDetail () {
    var topics = document.querySelectorAll('.content');
    var videos = document.querySelectorAll('.video_list');
    for(var i = 0; i < topics.length; i++){
        Tap(topics[i],function(e,dom){
            var cid = dom.parentNode.getAttribute('data-cid');
            var id = dom.parentNode.getAttribute('data-id');
            window.location.href = 'newcontent.html?cid='+ cid + '&id=' + id;
        });
    }
    for(var j = 0; j < videos.length; j++){
        Tap(videos[j],function(e,dom){
            var videoId = dom.getAttribute('data-video_id');
            window.location.href = 'videocontent.html?list=' + videoId;
        })
    }
}
window.onload = function () {
    changeIcon();
    touchst();

    var enter = document.querySelector("header a:last-child");
    Tap(enter,function(){
        if(selectByKey("like_token")){
            window.location.href = '../personal/individual.html';
        }else{
            window.location.href = '../personal/enter.html';
        }
    });
    var mySwiper = new Swiper ('.swiper-container', {
        direction: 'horizontal',
        loop: true,
        autoplay : 3000,
        autoplayDisableOnInteraction:false,
        // 如果需要分页器
        pagination: '.swiper-pagination',
    })

    $.ajax({
        url: pathName + '/api/v1/wedding',
        type: 'get',
        success: function (res) {
            console.log(res)
            //推荐话题
            ajaxHtml(res.tuijian.data,s('#listMode'),s('#fmain'));
            //热门话题
            ajaxHtml(res.remen.data,s('#listMode'),s('#rmain'));
            videoAjax(res.video.data, s('#videoList'), s('#video_wrapper'))
            toDetail ();
            s('.mask').style.display = 'none';
            shoucan();
        }
    })
};